package com.sy.sevice.impl;

import com.sy.dto.JsonResult;
import com.sy.entity.Course;
import com.sy.mapper.CourseMapper;
import com.sy.sevice.CourseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @author Monkey-D-Gu
 * @version 1.0
 * @date 2020/9/14 0014 15:03
 * @usage
 */
@Service
public class CourseServiceImpl implements CourseService {

    @Autowired
    private CourseMapper courseMapper;

    @Override
    @Transactional(isolation = Isolation.READ_COMMITTED, propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
    public JsonResult addCourse(Course course) {
        String name = course.getName();
        List<String> courseNameList = courseMapper.getAllCourseNames();
        if (null != courseNameList) {
            if (courseNameList.contains(name)) {
                return JsonResult.buildFailure(400,"课程名称已重复");
            }
        }
        boolean addSuccess = courseMapper.addCourse(course);
        System.out.println("是否添加成功：" + addSuccess);
        return JsonResult.buildSuccess(201,course);
    }
}
